// tbody.onclick = ;
tbody.addEventListener('click',function(e) {
    e = e || event;
    // console.log()
    if (e.target.innerText === "编辑") {
      if(loginState ==='on'){
        
      const editId= parseInt(e.target.parentNode.parentNode.getAttribute('data-index'))
      const trAll = document.querySelectorAll("tbody tr");
      const res = Array.from(trAll).indexOf(e.target.parentNode.parentNode);
    //   console.log(res)
      const aim = trAll[res];
    //   console.log(aim)
      inputname = aim.children[1].children[0].innerText;
      inputprice = aim.children[2].children[0].innerText;
      inputnum = aim.children[3].children[0].innerText;
      e.target.parentNode.parentNode.className = "edit";
      e.target.parentNode.parentNode.classList.add(`edit${res}`)
      const editInput = document.querySelectorAll(`.edit${res} input`);
      editInput[0].value = inputname;
      editInput[1].value = inputprice;
      editInput[2].value = inputnum;
      const btnok = document.querySelectorAll(".btn-ok")[res];
      const btncancel = document.querySelectorAll(".btn-cancel")[res];
      btnok.onclick = function(){
          const newName = editInput[0].value
          const newPrice = editInput[1].value
          const newNum = editInput[2].value
          const obj = {}
          obj.id = editId
          if(!(newName === inputname)){
              // console.log('不一样')
              obj.name = newName
              // console.log(obj)
          }
          if(!(newPrice === inputprice)){
              // console.log('不一样')
              obj.price = newPrice
              // console.log(obj)
          }
          if(!(newNum === inputnum)){
              // console.log('不一样')
              obj.num = newNum
              // console.log(obj)
          }
          if(newName === inputname&&newPrice === inputprice&&newNum === inputnum){
              e.target.parentNode.parentNode.className=""
          }else{
              utils.fetch('./api/v1/shop/edit.php',obj).then((resp)=>{
                  // console.log(e)
                  getData()
              })
              e.target.parentNode.parentNode.className = "";
          }
      }
      btncancel.onclick = function() {
        e.target.parentNode.parentNode.className = "";
      };
      }else{
        alert('请先登录')
      }
    }
  })